package com.newbsd.web.maventest.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.newbsd.web.maventest.common.pojo.EasyUIDataGridJSON;
import com.newbsd.web.maventest.pojo.User;
import com.newbsd.web.maventest.service.UserService;
import com.newbsd.web.maventest.service.impl.UserServiceImpl;

/**
 * @author Mei
 */
@WebServlet("/page")
public class pageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取页面显示的页数和行数
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		
		UserService userService = new UserServiceImpl();
		List<User> users = userService.queryUser(page, rows);
		
		//获取总条数
		int totalRecords = userService.countUser();
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(totalRecords);
		responseObject.setRows(users);
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(responseObject));
	
	}

}
